// File Name: CustomProxyFactory.cpp

#include "CustomProxyFactory.h"
#include "RotateClientSideCustomProvider.h"

namespace UIAutomationCPP
{
	CustomProxyFactory::CustomProxyFactory()
	{
	}

	CustomProxyFactory::~CustomProxyFactory()
	{
	}

	STDMETHODIMP CustomProxyFactory::CreateProvider(UIA_HWND hwnd, LONG idObject, LONG idChild, IRawElementProviderSimple **ppRetVal)
    {
        *ppRetVal = new RotateClientSideCustomProvider((HWND)hwnd);
        return S_OK;
    }

    STDMETHODIMP CustomProxyFactory::get_ProxyFactoryId(BSTR *pRetVal)
    {
        *pRetVal = SysAllocString(L"Custom Proxy Factory");
        return S_OK;
    }

	HRESULT __stdcall CustomProxyFactory::QueryInterface(REFIID riid, void **ppvObj)//riid == IID_IUIAutomationRegistrar, ppvObj == interface pointer to registrar
	{
		if(riid == IID_IUnknown)
			*ppvObj =(IUnknown*)((IRawElementProviderSimple*)this);
		else if(riid == IID_IRawElementProviderSimple)
			*ppvObj =(IRawElementProviderSimple*)this;
		else
		{
			*ppvObj = NULL;
			return E_NOINTERFACE;
		}

		((IUnknown*)(*ppvObj))->AddRef();
		return S_OK;
	}

	ULONG __stdcall CustomProxyFactory::AddRef()
	{
		InterlockedIncrement(&refCount);
		return refCount;
	}

	ULONG __stdcall CustomProxyFactory::Release()
	{
		ULONG ulRefCount = InterlockedDecrement(&refCount);
		if (ulRefCount == 0)
		{
			delete this;
		}
		return ulRefCount;
	}
}
